package yhp.com.zijie.web;


import yhp.com.zijie.bean.Student;
import yhp.com.zijie.service.StudentService;
import yhp.com.zijie.service.impl.StudentServiceImpl;
import yhp.com.zijie.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/studentList")
public class StudentListServlet extends HttpServlet {



    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取查询参数
        //获取查询参数
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        System.out.println(stuname);
        System.out.println(stuno);
        System.out.println(sex);

        //获取前端的当前页码
        String pageIndex = req.getParameter("pageIndex");

        //默认是第一页
        int index=pageIndex==null?1:Integer.parseInt(pageIndex);
        //如果没选择性别查询，则默认-1
        int usex=(sex==null||sex.length()==0?-1:Integer.parseInt(sex));


        //调用方法，获取学生信息
        PageUtil pageUtil = new PageUtil();

        StudentService service = new StudentServiceImpl();
        List<Student> allStudent = service.getAllStudent(stuname , stuno , usex , index , pageUtil.getPageSize());

        //获取总数
        int total = service.total(stuname, stuno,usex);
        pageUtil.setTotal(total);//赋给工具类中的，到时候其他地方调用的时候，就可以自己从工具类调用，节约资源

        //通过总数得到总页码数
//        int totalPages=total%5>0?total/5+1:total/5;
//        pageUtil.setTotalPages(totalPages);

        //存储dao层查询出的所有学生信息
//        req.setAttribute("allStudent" , allStudent);
        pageUtil.setDataList(allStudent);//分页的工具类里面包含的学生信息
        pageUtil.setPageIndex(index);//分页

        //存储前端查询的传来的值
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sex);

        //存储分页相关信息
        req.setAttribute("p1",pageUtil);
//        req.setAttribute("index",index);
//        req.setAttribute("total",total);
//        req.setAttribute("totalPages",totalPages);

        //跳转页面
        req.getRequestDispatcher("list.jsp").forward(req,resp);

    }
}
